﻿
using DataCollection.Core.Models;
using DataCollection.Infrastructure;
using DataCollection.WinForm.ViewModel;
using Masuit.Tools;

namespace DataCollection.WinForm.Forms
{
    public partial class AddDevicePointForm : DCBaseForm
    {
        #region 属性

        DevicePointViewModel viewModel = null!;

        /// <summary>
        /// 设备
        /// </summary>
        public DeviceItem deviceItem = null!;

        /// <summary>
        /// 需要绑定的控件
        /// </summary>
        private Dictionary<string, Control> ControlDict = new Dictionary<string, Control>();

        #endregion

        #region 自定义方法

        #endregion

        #region 系统事件
        public AddDevicePointForm()
        {
            InitializeComponent();
            Header.Text = "添加设备点位";
            ControlDict.Add("deviceNumber", DeviceNumberInput);
            ControlDict.Add("deviceName", DeviceNameInput);
            ControlDict.Add("deviceAddress", DeviceAddressInput);
            ControlDict.Add("number", PointNumberInput);
            ControlDict.Add("name", PointNameInput);
            ControlDict.Add("address", PointAddressInput);
            ControlDict.Add("pointType", RegisterTypeSelect);
            ControlDict.Add("rangeMin", RangeMinInput);
            ControlDict.Add("rangeMax", RangeMaxInput);
            ControlDict.Add("outMin", OutMinInput);
            ControlDict.Add("outMax", OutMaxInput);
        }

        private void AddDevicePointForm_Load(object sender, EventArgs e)
        {
            viewModel = new DevicePointViewModel(DateTimeOffset.Now.ToUnixTimeSeconds());
            foreach (var item in ControlDict)
            {
                Bindings(item.Value, "Text", item.Key, viewModel);
            }
            viewModel.DeviceNumber = deviceItem.Number;
            viewModel.DeviceName = deviceItem.Name;
            viewModel.DeviceAddress = deviceItem.SlaveAddress;
        }

        private void RegisterTypeSelect_SelectedValueChanged(object sender, AntdUI.ObjectNEventArgs e)
        {
            if (RegisterTypeSelect.Text == "DI" || RegisterTypeSelect.Text == "DO")
            {
                GroupBox3.Enabled = false;
            }
            else
            {
                GroupBox3.Enabled = true;
            }
        }
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (viewModel.Name.IsNullOrEmpty())
            {
                Error("请输入点位名称");
                return;
            }

            if (viewModel.Address.IsNullOrEmpty())
            {
                Error("请输入点位地址");
                return;
            }

            if (viewModel.PointType.IsNullOrEmpty())
            {
                Error("请选择点位类型");
                return;
            }
            viewModel.Apply();

            Response response = viewModel.Add();
            if (response.Code == Response.SuccessCode)
            {
                if (this.Owner != null)
                {
                    ManageDeviceForm frm = (ManageDeviceForm)this.Owner;
                    frm.LoadDevicePointList();
                    frm.Success(response.Msg);
                }
                this.Close();
            }
            else
            {
                Error(response.Msg);
                return;
            }
        }

        private void Input_Click(object sender, EventArgs e)
        {
            AntdUI.Input input = (AntdUI.Input)sender;
            KeyBoardForm.input = input;
            KeyBoardForm frm = new KeyBoardForm();
            frm.ShowDialog(this);
        }

        #endregion

    }
}
